<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Merchandise_model extends CI_Model 
{    
    function __construct()
    {
        parent::__construct();
    }
    
    function get_one($id)
    {
        $param['field'] = '*';
        
        $this->db->select($param['field']);
        $this->db->from('merchandise mcd');
        $this->db->join('mer_metas mm', 'mm.mer_id=mcd.mer_id', 'left');
        $this->db->join('metas m', 'mm.mer_id=m.mid', 'left');
        $this->db->join('users u', 'mcd.uid=u.uid', 'left');
        
        $this->db->where(array('mcd.mer_id'=>$id));
        $merchandise = $this->db->get()->row_array();
        
        if($isTag = $this->Tags->get_tag_merchandise_by_id($merchandise['mer_id']))
        {
            foreach($isTag as $tg)
            {
                $tgs[$tg['slug']] = $tg['name']; 
            }
            $merchandise['tags'] = $tgs;
        }
        
        return $merchandise;
    }
    
    /** 列表 */
    function get_merchandises(array $param, $page=0, $limit=20)
    {
        if(!isset($param['field']) || empty($param['field']))
        {
            $param['field'] = 'mcd.mer_id,mcd.mer_sttime,mcd.mer_sttime,mcd.mer_cdtime,mcd.mer_edtime,mcd.uid,mcd.mer_name,mcd.mer_content,mcd.mer_price,mcd.mer_moq,mcd.mer_total,mcd.mer_unit,mcd.mer_type,';
        }

        $this->db->select($param['field']);
        $this->db->from('merchandise mcd');
        //$this->db->join('user u', 'e.user_id=u.uid', 'left');
        //$this->db->join('relationships r', 'r.eid=e.eid', 'left');
        //$this->db->join('metas m', 'r.mid=m.mid', 'left');
        $this->db->join('users u', 'mcd.uid=u.uid', 'left');
        if(isset($param['where']) && ! empty($param['where']))
        {
            $this->db->where($param['where']);
        }
        if(isset($param['like']) && ! empty($param['like']))
        {
            $this->db->like($param['like']);
        }
        if(!isset($param['order']) || empty($param['order']))
        {
            $param['order']='mer_cdtime desc';
        }
        $this->db->order_by($param['order']);
        $this->db->limit($limit, $page);
        return $this->db->get()->result_array();
    }
    
    /** 插入数据 */
    function add($merchandise)
    {
        /** 构建插入结构 */
        $insertStruct = array(
            'mer_sttime'        =>  time(),
            'mer_cdtime'        =>  time(),
            'mer_edtime'        =>  is_numeric($merchandise['mer_edtime']) > 0?strtotime($merchandise['mer_edtime']):0,
            'uid'               =>  isset($merchandise['uid']) ? $merchandise['uid'] : $this->session->userdata('uid'),
            'mer_name'          =>  $merchandise['mer_name'],
            'mer_content'       =>  $merchandise['mer_content'],
            'mer_price'         =>  $merchandise['mer_price'],
            'mer_moq'           =>  $merchandise['mer_moq'],
            'mer_total'         =>  $merchandise['mer_total'],
            'mer_unit'          =>  empty($merchandise['mer_unit']) ? '公斤' : $merchandise['mer_unit'],
            'mer_type'          =>  empty($merchandise['mer_type']) ? 'supply' : $merchandise['mer_type'],
        );

        if (!empty($merchandise['mer_id']))
        {
            unset($insertStruct['mer_cdtime']);
            $this->db->where('mer_id', $merchandise['mer_id']);
            $insertId = $this->db->update('merchandise', $insertStruct);
        }
        else
        {
            /** 首先插入部分数据 */
            $this->db->insert('merchandise', $insertStruct);
            $insertId = $this->db->insert_id();
        }
        
        /** 处理附件 */
        if($insertId > 0 && isset($entries['parent']) && !empty($entries['parent']))
        {
            $this->load->model('Files_model');
            $this->Files_model->add_merchandise_files($insertId, $entries['parent']);
        }
        
        /** keyword */
        if($insertId > 0 && !empty($merchandise['tag']))
        {
            $this->load->model('Tags');
            $this->Tags->add_merchandise($insertId, $merchandise['tag']);
        }
        
        return $insertId;
    }
    
}